package com.youku.utils
{
   import flash.text.TextFormat;
   import flash.text.TextField;
   
   public class StringUtil extends Object
   {
      
      public function StringUtil() {
         super();
      }
      
      public static function replace(param1:String, param2:String, param3:String) : String {
         return param1.split(param2).join(param3);
      }
      
      public static function leftTrim(param1:String) : String {
         var _loc2_:uint = 0;
         while(_loc2_ < param1.length)
         {
            if(param1.substr(_loc2_,1) != " ")
            {
               return param1.substr(_loc2_);
            }
            if(_loc2_ == param1.length - 1)
            {
               return "";
            }
            _loc2_++;
         }
         return param1;
      }
      
      public static function rightTrim(param1:String) : String {
         var _loc2_:uint = param1.length - 1;
         while(_loc2_ >= 0)
         {
            if(param1.substr(_loc2_,1) != " ")
            {
               return param1.substr(0,_loc2_ + 1);
            }
            if(_loc2_ == 0)
            {
               return "";
            }
            _loc2_--;
         }
         return param1;
      }
      
      public static function trim(param1:String) : String {
         var _loc2_:String = param1;
         _loc2_ = leftTrim(_loc2_);
         _loc2_ = rightTrim(_loc2_);
         return _loc2_;
      }
      
      public static function trimAll(param1:String) : String {
         return param1.split(" ").join("");
      }
      
      public static function trimXML(param1:String) : String {
         var _loc2_:* = "";
         var _loc3_:Array = param1.split(">");
         var _loc4_:uint = 0;
         while(_loc4_ < _loc3_.length - 1)
         {
            _loc3_[_loc4_] = trim(_loc3_[_loc4_] + ">");
            _loc2_ = _loc2_ + _loc3_[_loc4_];
            _loc4_++;
         }
         return _loc2_;
      }
      
      public static function leftStr(param1:String, param2:int) : String {
         if(param1.length > param2)
         {
            param1 = param1.substring(0,param2);
         }
         return param1;
      }
      
      public static function rightStr(param1:String, param2:int) : String {
         if(param1.length > param2)
         {
            param1 = param1.substr(-param2);
         }
         return param1;
      }
      
      public static function insertStr(param1:String, param2:String, param3:int) : String {
         var _loc4_:String = leftStr(param1,param3);
         var _loc5_:String = rightStr(param1,param1.length - param3);
         var _loc6_:String = _loc4_ + param2 + _loc5_;
         return _loc6_;
      }
      
      public static function subfix(param1:String) : String {
         var param1:String = trim(param1.toLowerCase());
         param1 = param1.substring(param1.lastIndexOf(".") + 1,param1.length);
         return param1;
      }
      
      public static function isEmail(param1:String) : Boolean {
         if(param1 == null)
         {
            return false;
         }
         var param1:String = trim(param1);
         var _loc2_:RegExp = new RegExp("(\\w|[_.\\-])+@((\\w|-)+\\.)+\\w{2,4}+");
         if(_loc2_.exec(param1) == null)
         {
            return false;
         }
         return true;
      }
      
      public static function getGUID() : String {
         var _loc3_:* = 0;
         var _loc4_:* = 0;
         var _loc1_:* = "";
         var _loc2_:* = "0123456789abcdef";
         _loc3_ = 0;
         while(_loc3_ < 8)
         {
            _loc1_ = _loc1_ + _loc2_.charAt(Math.round(Math.random() * 15));
            _loc3_++;
         }
         _loc3_ = 0;
         while(_loc3_ < 3)
         {
            _loc1_ = _loc1_ + "-";
            _loc4_ = 0;
            while(_loc4_ < 4)
            {
               _loc1_ = _loc1_ + _loc2_.charAt(Math.round(Math.random() * 15));
               _loc4_++;
            }
            _loc3_++;
         }
         _loc1_ = _loc1_ + "-";
         var _loc5_:Number = new Date().getTime();
         _loc1_ = _loc1_ + ("0000000" + _loc5_.toString(16).toUpperCase()).substr(-8);
         _loc3_ = 0;
         while(_loc3_ < 4)
         {
            _loc1_ = _loc1_ + _loc2_.charAt(Math.round(Math.random() * 15));
            _loc3_++;
         }
         return _loc1_;
      }
      
      public static function URLJoin(param1:String, param2:String) : String {
         var _loc3_:* = param1.lastIndexOf("/") == param1.length - 1;
         var _loc4_:* = param2.indexOf("/") == 0;
         if((_loc4_) && (_loc3_))
         {
            return param1 + param2.substr(1);
         }
         if((_loc4_) || (_loc3_))
         {
            return param1 + param2;
         }
         return [param1,param2].join("/");
      }
      
      public static function commaWithNumber(param1:uint) : String {
         var _loc2_:String = String(param1);
         var _loc3_:Array = _loc2_.split("");
         var _loc4_:Array = [];
         _loc3_.reverse();
         var _loc5_:* = 0;
         while(_loc5_ < _loc2_.length)
         {
            if(_loc5_ != 0)
            {
               if(_loc5_ % 3 == 0)
               {
                  _loc4_.push(",");
               }
            }
            _loc4_.push(_loc3_[_loc5_]);
            _loc5_++;
         }
         _loc4_.reverse();
         return _loc4_.join("");
      }
      
      public static function getDomain(param1:String) : String {
         var url:String = param1;
         var domain:String = "";
         var pattern:RegExp = new RegExp("^(?:(\\w+):\\/\\/)?(?:(\\w+):?(\\w+)?@)?([^:\\/\\?#]+)(?::(\\d+))?(\\/[^\\?#]+)?(?:\\?([^#]+))?(?:#(\\w+))?");
         try
         {
            domain = url.match(pattern)[4];
         }
         catch(e:Error)
         {
            domain = "";
         }
         if(!domain)
         {
            domain = "";
         }
         return domain;
      }
      
      public static function isDomainMatch(param1:String, param2:String) : Boolean {
         if(param1 == null || param2 == null)
         {
            return false;
         }
         var _loc3_:int = param1.length;
         var _loc4_:int = param2.length;
         var _loc5_:* = 1;
         while(_loc5_ <= param2.length)
         {
            if(_loc3_ - _loc5_ < 0 || !(param2.charAt(_loc4_ - _loc5_) == param1.charAt(_loc3_ - _loc5_)))
            {
               return false;
            }
            _loc5_++;
         }
         return true;
      }
      
      public static function isDomianLegal(param1:String) : Boolean {
         var _loc2_:String = getDomain(param1);
         if(_loc2_ == null || _loc2_ == "")
         {
            return false;
         }
         return (isDomainMatch(_loc2_,".youku.com")) || (isDomainMatch(_loc2_,".tudou.com")) || (isDomainMatch(_loc2_,".ykimg.com"));
      }
      
      public static function getLimitWidthStr(param1:String, param2:Number, param3:TextFormat) : String {
         var _loc4_:TextField = new TextField();
         _loc4_.defaultTextFormat = param3;
         _loc4_.text = param1;
         if(_loc4_.textWidth <= param2)
         {
            return param1;
         }
         var _loc5_:Number = 1;
         while(_loc5_ <= param1.length)
         {
            _loc4_.text = param1.substr(0,_loc5_);
            if(_loc4_.textWidth > param2)
            {
               _loc5_--;
               break;
            }
            _loc5_++;
         }
         if(_loc5_ < param1.length)
         {
            param1 = param1.substr(0,_loc5_);
            param1 = param1 + "...";
         }
         return param1;
      }
   }
}
